import Vue from 'vue'
import VueRouter from 'vue-router'

import Home from '../views/Home.vue'
import Login from '../views/Login.vue'
import Register from '../views/Register.vue'
import ProfileIndex from '../views/profile/index.vue'
import ProfileEdit from '../views/profile/ProfileEdit.vue'

import Test from '../views/Test.vue'

Vue.use(VueRouter)

const routes = [
  {
    path: '/',
    component: Home
  },
  {
    path: '/login',
    component: Login
  },
  {
    path: '/register',
    component: Register
  },
  {
    path: "/profileindex",
    component: ProfileIndex
  },
  {
    path: '/profileedit',
    component: ProfileEdit
  },
  {
    path: '/test',
    component: Test
  }
]


const router = new VueRouter({
  routes
})

// 添加全局路路由守卫：
router.beforeEach((to, from, next) => {
  if (to.path == '/profileindex') {
    //判断token是否存在
    if (localStorage.getItem('token')) {
      return next()
    } else {
      return router.push('/login').catch((err) => {

      })

    }
  }

  return next()
})




export default router
